/**
 * @param {number[][]} boxTypes
 * @param {number} truckSize
 * @return {number}
 */
var maximumUnits = function (boxTypes, truckSize) {
  boxTypes = boxTypes.sort((a, b) => {
    if(b[1]!=a[1]){
      return b[1]-a[1]
    }else {
      return a[0]-b[0]
    }
  })
  let res = 0
  for(let i=0;i<boxTypes.length;i++){
 if(truckSize-boxTypes[i][0]>0){
      truckSize-=boxTypes[i][0]
      res+=boxTypes[i][0]*boxTypes[i][1]
    }else {
      res+=truckSize*boxTypes[i][1]
      truckSize =0
    }
  }
  return res
}

console.log(maximumUnits([[2,1],[4,4],[3,1],[4,1],[2,4],[3,4],[1,3],[4,3],[5,3],[5,3]],13));
